<?php
class TreeItem extends DomArObject {
	
	/**
	 * @orm text
	 */
	public $name;
	/**
	 * @orm owns many TreeItem inverse parent
	 */
	public $children;
	/**
	 * @orm has parent TreeItem inverse children
	 */
	public $parent;
	
	
	public static function createSampleData() {
		
		TreeItem::getTable()->update();
		
		$item = TreeItem::query()->limit(1)->commit();
		if( $item )
			return;

			
		$list = array(
			'item 1' => array(
				'item 1.1',
				'item 1.2' => array(
					'item 1.2.1',
					'item 1.2.2'
				),
				'item 1.3',
			),
			'item 2' => array(
				'item 2.1',
				'item 2.2' => array(
					'item 2.2.1',
					'item 2.2.2'
				),
				'item 2.3',
				'item 2.4' => array(
					'item 2.4.1',
					'item 2.4.2'
				),
			),
		);
		
		function __TreeItemHelper( array $list, $parent ) {
			$rList = array();
			foreach( $list as $key => $value ) {
				$item = new TreeItem();
				if( is_string($value) ) {
					$item->name = $value;
				} else {
					$item->name = $key;
					__TreeItemHelper( $value, $item );
				}
				if( $parent ) 
					$parent->children[] = $item;
				
				$rList[] = $item;
			}
			return $rList;
		}
		
		
		foreach( __TreeItemHelper($list, NULL) as $e ) {
			$e->save();
		}
		
		

		
	}
}
?>